/*
 * Copyright 2010 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.valdemar.android.givemeanswers;

import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.ServerFailure;

import com.valdemar.android.givemeanswers.client.MyRequestFactory;
import com.valdemar.android.givemeanswers.client.MyRequestFactory.HelloWorldRequest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * Main activity - requests "Hello, World" messages from the server and provides
 * a menu item to invoke the accounts activity.
 */
public class GiveMeAnswersActivity extends BaseActivity
{
	/**
	 * Tag for logging.
	 */
	private static final String TAG = "GiveMeAnswersActivity";

	/**
	 * The current context.
	 */
	private Context mContext = this;

	/**
	 * A {@link BroadcastReceiver} to receive the response from a register or
	 * unregister request, and to update the UI.
	 */
	private final BroadcastReceiver mUpdateUIReceiver = new BroadcastReceiver()
	{
		@Override
		public void onReceive(Context context, Intent intent)
		{
			String accountName = intent.getStringExtra(DeviceRegistrar.ACCOUNT_NAME_EXTRA);
			int status = intent.getIntExtra(DeviceRegistrar.STATUS_EXTRA,
					DeviceRegistrar.ERROR_STATUS);
			String message = null;
			String connectionStatus = Util.DISCONNECTED;
			if (status == DeviceRegistrar.REGISTERED_STATUS)
			{
				message = getResources().getString(R.string.registration_succeeded);
				connectionStatus = Util.CONNECTED;
			}
			else if (status == DeviceRegistrar.UNREGISTERED_STATUS)
			{
				message = getResources().getString(R.string.unregistration_succeeded);
			}
			else
			{
				message = getResources().getString(R.string.registration_error);
			}

			// Set connection status
			SharedPreferences prefs = Util.getSharedPreferences(mContext);
			prefs.edit().putString(Util.CONNECTION_STATUS, connectionStatus).commit();

			// Display a notification
			Util.generateNotification(mContext, String.format(message, accountName));
		}
	};

	/**
	 * Begins the activity.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		Log.i(TAG, "onCreate");
		super.onCreate(savedInstanceState);

		// Register a receiver to provide register/unregister notifications
		registerReceiver(mUpdateUIReceiver, new IntentFilter(Util.UPDATE_UI_INTENT));
		
		setContentView(R.layout.hello_world);
		
		setHelloWorldScreenContent();
	}



	/**
	 * Shuts down the activity.
	 */
	@Override
	public void onDestroy()
	{
		unregisterReceiver(mUpdateUIReceiver);
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		// Invoke the Register activity
		menu.getItem(0).setIntent(new Intent(this, AccountsActivity.class));
		return true;
	}

	// Manage UI Screens

	private void setHelloWorldScreenContent()
	{

		final TextView helloWorld = (TextView) findViewById(R.id.hello_world);
		final Button sayHelloButton = (Button) findViewById(R.id.say_hello);
		sayHelloButton.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				sayHelloButton.setEnabled(false);
				helloWorld.setText(R.string.contacting_server);

				// Use an AsyncTask to avoid blocking the UI thread
				new AsyncTask<Void, Void, String>()
				{
					private String message;

					@Override
					protected String doInBackground(Void... arg0)
					{
						MyRequestFactory requestFactory = Util.getRequestFactory(mContext,
								MyRequestFactory.class);
						final HelloWorldRequest request = requestFactory.helloWorldRequest();
						Log.i(TAG, "Sending request to server");
						request.getMessage().fire(new Receiver<String>()
						{
							@Override
							public void onFailure(ServerFailure error)
							{
								message = "Failure: " + error.getMessage();
							}

							@Override
							public void onSuccess(String result)
							{
								message = result;
							}
						});
						return message;
					}

					@Override
					protected void onPostExecute(String result)
					{
						helloWorld.setText(result);
						sayHelloButton.setEnabled(true);
					}
				}.execute();
			}
		});
	}

	
}
